home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / shlex.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-13  |  6.8 KB  |  311 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import os.path as os
  5. import sys
  6. from collections import deque
  7.  
  8. try:
  9.     from cStringIO import StringIO
  10. except ImportError:
  11.     from StringIO import StringIO
  12.  
  13. __all__ = [
  14.     'shlex',
  15.     'split']
  16.  
  17. class shlex:
  18.     
  19.     def __init__(self, instream = None, infile = None, posix = False):
  20.         if isinstance(instream, basestring):
  21.             instream = StringIO(instream)
  22.         
  23.         if instream is not None:
  24.             self.instream = instream
  25.             self.infile = infile
  26.         else:
  27.             self.instream = sys.stdin
  28.             self.infile = None
  29.         self.posix = posix
  30.         if posix:
  31.             self.eof = None
  32.         else:
  33.             self.eof = ''
  34.         self.commenters = '#'
  35.         self.wordchars = 'abcdfeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
  36.         if self.posix:
  37.             self.wordchars += '\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde'
  38.         
  39.         self.whitespace = ' \t\r\n'
  40.         self.whitespace_split = False
  41.         self.quotes = '\'"'
  42.         self.escape = '\\'
  43.         self.escapedquotes = '"'
  44.         self.state = ' '
  45.         self.pushback = deque()
  46.         self.lineno = 1
  47.         self.debug = 0
  48.         self.token = ''
  49.         self.filestack = deque()
  50.         self.source = None
  51.         if self.debug:
  52.             print 'shlex: reading from %s, line %d' % (self.instream, self.lineno)
  53.         
  54.  
  55.     
  56.     def push_token(self, tok):
  57.         if self.debug >= 1:
  58.             print 'shlex: pushing token ' + repr(tok)
  59.         
  60.         self.pushback.appendleft(tok)
  61.  
  62.     
  63.     def push_source(self, newstream, newfile = None):
  64.         if isinstance(newstream, basestring):
  65.             newstream = StringIO(newstream)
  66.         
  67.         self.filestack.appendleft((self.infile, self.instream, self.lineno))
  68.         self.infile = newfile
  69.         self.instream = newstream
  70.         self.lineno = 1
  71.         if self.debug:
  72.             if newfile is not None:
  73.                 print 'shlex: pushing to file %s' % (self.infile,)
  74.             else:
  75.                 print 'shlex: pushing to stream %s' % (self.instream,)
  76.         
  77.  
  78.     
  79.     def pop_source(self):
  80.         self.instream.close()
  81.         (self.infile, self.instream, self.lineno) = self.filestack.popleft()
  82.         if self.debug:
  83.             print 'shlex: popping to %s, line %d' % (self.instream, self.lineno)
  84.         
  85.         self.state = ' '
  86.  
  87.     
  88.     def get_token(self):
  89.         if self.pushback:
  90.             tok = self.pushback.popleft()
  91.             if self.debug >= 1:
  92.                 print 'shlex: popping token ' + repr(tok)
  93.             
  94.             return tok
  95.         
  96.         raw = self.read_token()
  97.         if self.source is not None:
  98.             while raw == self.source:
  99.                 spec = self.sourcehook(self.read_token())
  100.                 if spec:
  101.                     (newfile, newstream) = spec
  102.                     self.push_source(newstream, newfile)
  103.                 
  104.                 raw = self.get_token()
  105.         
  106.         while raw == self.eof:
  107.             if not self.filestack:
  108.                 return self.eof
  109.                 continue
  110.             self.pop_source()
  111.             raw = self.get_token()
  112.         if self.debug >= 1:
  113.             if raw != self.eof:
  114.                 print 'shlex: token=' + repr(raw)
  115.             else:
  116.                 print 'shlex: token=EOF'
  117.         
  118.         return raw
  119.  
  120.     
  121.     def read_token(self):
  122.         quoted = False
  123.         escapedstate = ' '
  124.         while True:
  125.             nextchar = self.instream.read(1)
  126.             if nextchar == '\n':
  127.                 self.lineno = self.lineno + 1
  128.             
  129.             if self.debug >= 3:
  130.                 print 'shlex: in state', repr(self.state), 'I see character:', repr(nextchar)
  131.             
  132.             if self.state is None:
  133.                 self.token = ''
  134.                 break
  135.                 continue
  136.             if self.state == ' ':
  137.                 if not nextchar:
  138.                     self.state = None
  139.                     break
  140.                 elif nextchar in self.whitespace:
  141.                     if self.debug >= 2:
  142.                         print 'shlex: I see whitespace in whitespace state'
  143.                     
  144.                     if (self.token or self.posix) and quoted:
  145.                         break
  146.                     
  147.                 elif nextchar in self.commenters:
  148.                     self.instream.readline()
  149.                     self.lineno = self.lineno + 1
  150.                 elif self.posix and nextchar in self.escape:
  151.                     escapedstate = 'a'
  152.                     self.state = nextchar
  153.                 elif nextchar in self.wordchars:
  154.                     self.token = nextchar
  155.                     self.state = 'a'
  156.                 elif nextchar in self.quotes:
  157.                     if not self.posix:
  158.                         self.token = nextchar
  159.                     
  160.                     self.state = nextchar
  161.                 elif self.whitespace_split:
  162.                     self.token = nextchar
  163.                     self.state = 'a'
  164.                 else:
  165.                     self.token = nextchar
  166.                     if (self.token or self.posix) and quoted:
  167.                         break
  168.                     
  169.             nextchar in self.escape
  170.             if self.state in self.quotes:
  171.                 quoted = True
  172.                 if not nextchar:
  173.                     if self.debug >= 2:
  174.                         print 'shlex: I see EOF in quotes state'
  175.                     
  176.                     raise ValueError, 'No closing quotation'
  177.                 
  178.                 if nextchar == self.state:
  179.                     if not self.posix:
  180.                         self.token = self.token + nextchar
  181.                         self.state = ' '
  182.                         break
  183.                     else:
  184.                         self.state = 'a'
  185.                 elif self.posix and nextchar in self.escape and self.state in self.escapedquotes:
  186.                     escapedstate = self.state
  187.                     self.state = nextchar
  188.                 else:
  189.                     self.token = self.token + nextchar
  190.             self.state in self.escapedquotes
  191.             if self.state in self.escape:
  192.                 if not nextchar:
  193.                     if self.debug >= 2:
  194.                         print 'shlex: I see EOF in escape state'
  195.                     
  196.                     raise ValueError, 'No escaped character'
  197.                 
  198.                 if escapedstate in self.quotes and nextchar != self.state and nextchar != escapedstate:
  199.                     self.token = self.token + self.state
  200.                 
  201.                 self.token = self.token + nextchar
  202.                 self.state = escapedstate
  203.                 continue
  204.             if self.state == 'a':
  205.                 if not nextchar:
  206.                     self.state = None
  207.                     break
  208.                 elif nextchar in self.whitespace:
  209.                     if self.debug >= 2:
  210.                         print 'shlex: I see whitespace in word state'
  211.                     
  212.                     self.state = ' '
  213.                     if (self.token or self.posix) and quoted:
  214.                         break
  215.                     
  216.                 elif nextchar in self.commenters:
  217.                     self.instream.readline()
  218.                     self.lineno = self.lineno + 1
  219.                     if self.posix:
  220.                         self.state = ' '
  221.                         if (self.token or self.posix) and quoted:
  222.                             break
  223.                         
  224.                     
  225.                 elif self.posix and nextchar in self.quotes:
  226.                     self.state = nextchar
  227.                 elif self.posix and nextchar in self.escape:
  228.                     escapedstate = 'a'
  229.                     self.state = nextchar
  230.                 elif nextchar in self.wordchars and nextchar in self.quotes or self.whitespace_split:
  231.                     self.token = self.token + nextchar
  232.                 else:
  233.                     self.pushback.appendleft(nextchar)
  234.                     if self.debug >= 2:
  235.                         print 'shlex: I see punctuation in word state'
  236.                     
  237.                     self.state = ' '
  238.                     if self.token:
  239.                         break
  240.                     
  241.             self.whitespace_split
  242.         result = self.token
  243.         self.token = ''
  244.         if self.posix and not quoted and result == '':
  245.             result = None
  246.         
  247.         if self.debug > 1:
  248.             if result:
  249.                 print 'shlex: raw token=' + repr(result)
  250.             else:
  251.                 print 'shlex: raw token=EOF'
  252.         
  253.         return result
  254.  
  255.     
  256.     def sourcehook(self, newfile):
  257.         if newfile[0] == '"':
  258.             newfile = newfile[1:-1]
  259.         
  260.         if isinstance(self.infile, basestring) and not os.path.isabs(newfile):
  261.             newfile = os.path.join(os.path.dirname(self.infile), newfile)
  262.         
  263.         return (newfile, open(newfile, 'r'))
  264.  
  265.     
  266.     def error_leader(self, infile = None, lineno = None):
  267.         if infile is None:
  268.             infile = self.infile
  269.         
  270.         if lineno is None:
  271.             lineno = self.lineno
  272.         
  273.         return '"%s", line %d: ' % (infile, lineno)
  274.  
  275.     
  276.     def __iter__(self):
  277.         return self
  278.  
  279.     
  280.     def next(self):
  281.         token = self.get_token()
  282.         if token == self.eof:
  283.             raise StopIteration
  284.         
  285.         return token
  286.  
  287.  
  288.  
  289. def split(s, comments = False):
  290.     lex = shlex(s, posix = True)
  291.     lex.whitespace_split = True
  292.     if not comments:
  293.         lex.commenters = ''
  294.     
  295.     return list(lex)
  296.  
  297. if __name__ == '__main__':
  298.     if len(sys.argv) == 1:
  299.         lexer = shlex()
  300.     else:
  301.         file = sys.argv[1]
  302.         lexer = shlex(open(file), file)
  303.     while None:
  304.         tt = lexer.get_token()
  305.         if tt:
  306.             print 'Token: ' + repr(tt)
  307.             continue
  308.         break
  309.         continue
  310. __name__ == '__main__'
  311.